package veluanasoft.vista;


import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import veluanasoft.control.InvoiceControl;
import veluanasoft.modelo.Invoice;
import veluanasoft.modelo.Item;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * IngresoClientePanel.java
 *
 * Created on 15/03/2012, 05:45:40 PM
 */

/**
 *
 * @author Juan
 */
public class IngresoClientePanel extends javax.swing.JPanel {

    double[][] registro;
    Invoice invoice;
    InvoiceControl invoiceControl;

    /** Creates new form IngresoClientePanel */
    public IngresoClientePanel(Invoice invoice, InvoiceControl invoiceControl) {
        initComponents();
        registro = new double[3][5];
        this.invoice = invoice;
        this.invoiceControl = invoiceControl;
        SliderCantidad.getValue();
        setSize(500, 400);
        jTextArea1.setEditable(false);
        for (int i = 0; i<3;i++)
            for (int j = 0; j<5;j++)
                registro[i][j] = 0;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        SliderCantidad = new javax.swing.JSlider();
        lstItem = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        cantidadJL = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jToggleButton1 = new javax.swing.JToggleButton();

        setLayout(null);

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });
        add(jTextField1);
        jTextField1.setBounds(110, 30, 300, 20);

        jLabel1.setText("Nombre:");
        add(jLabel1);
        jLabel1.setBounds(40, 30, 70, 14);
        add(jTextField2);
        jTextField2.setBounds(110, 70, 162, 20);

        jLabel2.setText("Direccion:");
        add(jLabel2);
        jLabel2.setBounds(40, 70, 70, 14);

        SliderCantidad.setMaximum(500);
        SliderCantidad.setValue(0);
        SliderCantidad.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                SliderCantidadStateChanged(evt);
            }
        });
        add(SliderCantidad);
        SliderCantidad.setBounds(180, 100, 160, 23);

        lstItem.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Cherry", "Curly Maple", "Genuine Mahugany", "Wenge", "White Oak" }));
        add(lstItem);
        lstItem.setBounds(40, 100, 117, 20);

        jButton1.setText("Agregar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        add(jButton1);
        jButton1.setBounds(190, 150, 100, 23);

        cantidadJL.setText("0");
        add(cantidadJL);
        cantidadJL.setBounds(370, 100, 30, 14);

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        add(jScrollPane1);
        jScrollPane1.setBounds(30, 190, 410, 160);

        jToggleButton1.setText("Facturar");
        jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton1ActionPerformed(evt);
            }
        });
        add(jToggleButton1);
        jToggleButton1.setBounds(180, 370, 110, 23);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        double bfs = (double)SliderCantidad.getValue();
        int woodTypeI = lstItem.getSelectedIndex();
        String woodTypeS = String.valueOf(lstItem.getSelectedItem());
        Item item;

        registro[0][woodTypeI] += bfs;
        item = new Item(woodTypeS, (int)registro[0][woodTypeI]);
        
        registro[1][woodTypeI] = item.getPriceOfWood();
        registro[2][woodTypeI] = item.getSubTotalPrice();

        jTextArea1.setText("Item\t\tQuatity\tPrice\tCost\n"
                + "Cherry\t\t"+registro[0][0]+"\t"+registro[1][0]+"\t"+registro[2][0]+"\n"
                + "Curly Maple\t\t"+registro[0][1]+"\t"+registro[1][1]+"\t"+registro[2][1]+"\n"
                + "Genuine Mahogany\t"+registro[0][2]+"\t"+registro[1][2]+"\t"+registro[2][2]+"\n"
                + "Wenge\t\t"+registro[0][3]+"\t"+registro[1][3]+"\t"+registro[2][3]+"\n"
                + "White Oak\t\t"+registro[0][4]+"\t"+registro[1][4]+"\t"+registro[2][4]+"\n\n"
                + "Precio Total:\t"+precioTotal()+"\n");

    }//GEN-LAST:event_jButton1ActionPerformed

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1ActionPerformed

    private void SliderCantidadStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_SliderCantidadStateChanged
        // TODO add your handling code here:
        cantidadJL.setText(String.valueOf(SliderCantidad.getValue()));
    }//GEN-LAST:event_SliderCantidadStateChanged

    private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton1ActionPerformed
        // TODO add your handling code here:
        String[] woods = new String[5];
        woods[0] = "Cherry";
        woods[1] = "Curly Maple";
        woods[2] = "Genuine Mahugany";
        woods[3] = "Wenge";
        woods[4] = "White Oak";
        invoice = new Invoice(jTextField1.getText(), jTextField2.getText());
        for (int i=0;i<5;i++)
            invoice.addItem(woods[i], (int)registro[0][i]);
        new Factura(invoice, invoiceControl).setVisible(true);
    }//GEN-LAST:event_jToggleButton1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSlider SliderCantidad;
    private javax.swing.JLabel cantidadJL;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JComboBox lstItem;
    // End of variables declaration//GEN-END:variables

    private double precioTotal() {
        double precioTotal;
        precioTotal = 0;

        for (int i=0;i<5;i++)
            precioTotal += registro[2][i];

        return precioTotal;
    }

}
